 /********************************************************************
 
 Copyright (C), 2012年 dhc. All rights reserved.
 
 Filename: 	    MailList.m 
 
 Author:        muzongcun
 
 Version:       1.0
 
 Description:   maillist
 
 History:
 
 muzongcun    2012/3/10     1.0     Create
 
 ********************************************************************/

#import "MailList.h"
#import "MailListCell.h"
#import "TaskManager.h"
#import "AppDelegate.h"
#import "NavigationMainViewController.h"
#import "MailMessageComposer.h"
#import "MailDetailMessage.h"
#import "XmlRpc.h"
#import "Session.h"
#import "myToast.h"
@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect 
{
    CGRect rectTemp = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rectTemp.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [NAVIGATION_BAR_COLOR CGColor]);
    CGContextFillRect(context, rectTemp);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    
   // UIImage *image =  [UIImage imageNamed:PIC_MAIL_TOP_BAR];
   // [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    
} 
@end

@implementation MailList

@synthesize m_table;
@synthesize m_toolDrawerView;
@synthesize m_mailNumTotal;
@synthesize m_searchText;
@synthesize m_searchDisplayController;
@synthesize m_mailSearchBar;
@synthesize m_checkedArray;
@synthesize m_importantArray;
@synthesize m_barTitleLabel;
@synthesize m_employeeID;
@synthesize m_mutableArray;
@synthesize m_selectClientID;
@synthesize m_bIsSearching;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self)
    {
        m_checkedArray      =   [[NSMutableArray alloc] init];
        m_importantArray    =   [[NSMutableArray alloc] init];
        m_selectClientID    =   nil;
        m_searchScopeType   =   @"01";          //默认识，按照邮件标题搜索
        m_searchText        =   nil;            //查找文本
        m_refreshType       =   NORMAL_FRESH;
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(id) init
{
    self = [super init];
	if(self)
	{
        m_mutableArray = [[NSMutableArray alloc] init];
    }    
    return self;    
}

- (void)dealloc
{
    [m_checkedArray release];
    [m_importantArray release];
    [m_mutableArray release];
    [m_table release];
    [m_toolDrawerView release];
    [m_employeeID release];
    
    [m_barTitleLabel release];    //箱体类型名字
    
    [m_toolDrawerView release];   //弹出菜单的控件
    [m_mutableArray release];     //动态存放所有maillist中数据
    [m_mailNumTotal release];     //删除合并邮件数量累价
    [m_checkedArray release];     //动态存放选中数据
    [m_importantArray release];   //设置是否重要数据
    if (m_searchText != nil)
    {
        [m_searchText release];
    }
    
    [m_navBar release];           //自己创建
    [m_mailSearchBar release];    //查找bar
    if (m_employeeID != nil) 
    {
        [m_employeeID release];  //员工id
    }
   
    [m_selectClientID release];   //保存选择的客户id
    [m_searchScopeType release];  //查找不同的类型（发件人和邮件标题）
    
    [m_searchDisplayController release];
}

#pragma mark -
#pragma mark - my function
//******************************************************
// Method:     refresh
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   刷新
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void)refresh
{
    
}

//******************************************************
// Method:     SetMailListType
// Access:     public 
// Returns:    void
// Parameter:  int
// Note:	   设置maillist类型
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void)SetMailListType:(int)type
{
    m_mailListType = type;
}


#pragma mark - View lifecycle
//******************************************************
// Method:     onDelete
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   删除选中的邮件
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void) onDelete
{
    NSString* shareIdList = NULL;
    NSMutableArray *allListArray = m_mutableArray;
    //得到删除邮件的数量
    NSInteger nDelMailNum = [m_checkedArray count];
    for (int i=0; i<[allListArray count]; i++) 
    {
        NSMutableDictionary *listCellDic = [allListArray objectAtIndex:i];
        
        NSString* listCellTag = [listCellDic objectForKey:@"cellTag"];
        
        
        NSMutableArray *tagArray = [[NSMutableArray alloc] initWithArray:m_checkedArray];
        for (NSMutableDictionary* tagDic in tagArray)
        {
            NSString* strCellTag = [tagDic objectForKey:@"cellTag"];
            if ([listCellTag compare:strCellTag] == 0) 
            {
                NSString* strID = [listCellDic objectForKey:@"id"];
                NSString *str = [NSString stringWithFormat:@"%@,%@",shareIdList,strID];
                shareIdList = str;
                
                [allListArray removeObject:listCellDic];
                i--;
                break;
            }
        }
        [tagArray release];
    }
    //字符串从第一个字符开始截取
    NSString* temp = [shareIdList substringFromIndex:7];
    
    //删除选中的数据
    XmlRpc *xml = [[XmlRpc alloc] init];
    BOOL bSuccess  = [xml deleteMail:temp];
    if (bSuccess) 
    {
        NSString* strToast = [NSString stringWithFormat:@"%d封邮件删除成功", nDelMailNum];
        [myToast showWithText:strToast];
    }
   
    [m_checkedArray removeAllObjects];
    [xml release];
    
    //删除后，浮动条隐藏
    [self onHide];
    [self.m_table reloadData];
}

//******************************************************
// Method:     onMerge
// Access:     public 
// Returns:    void
// Parameter:  int
// Note:	   合并邮件
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void) onMerge
{

    ///////////////////////////////////////////
    int nNotMergeCount = 0;
    bool bIsExist = NO;
    NSString* shareIdList = NULL;
    NSMutableArray *allListArray = m_mutableArray;
    
    //保存需要删除的数据
    for (int i=0; i<[allListArray count]; i++) 
    {
        NSMutableDictionary *listCellDic = [allListArray objectAtIndex:i];
        
        NSString* listCellTag = [listCellDic objectForKey:@"cellTag"];
        
        //遍历所有的list，取得所有选择的shareid
        NSMutableArray *tagArray = [[NSMutableArray alloc] initWithArray:m_checkedArray];
        for (NSMutableDictionary* tagDic in tagArray)
        {
            NSString* strCellTag = [tagDic objectForKey:@"cellTag"];
            if ([listCellTag compare:strCellTag] == 0) 
            {
                NSString* strID = [listCellDic objectForKey:@"id"];
                NSString *str = [NSString stringWithFormat:@"%@,%@",shareIdList,strID];
                shareIdList = str;
                break;
            }
        }
        [tagArray release];
    }
    //字符串从第一个字符开始截取
    NSString* temp = [shareIdList substringFromIndex:7];
    
    /*归并邮件*/  
    NSMutableDictionary * mergeNewMailParm = [[[NSMutableDictionary alloc] initWithObjectsAndKeys:temp,@"mail_share_ids", nil] autorelease];
    
    XmlRpc *xml = [[XmlRpc alloc] init];
    NSMutableDictionary* dicRet = [xml settingMultiMerger:mergeNewMailParm];
    if (dicRet == nil)
    {
        return;
    }
    //返回不能归并的邮件地址
    NSString*  error_message = [dicRet objectForKey:@"error_message"]; 
    NSArray *  arrayMerge   = [error_message componentsSeparatedByString:@","];
    NSLog(@"%@",error_message);
    
    //根据返回值，如果有返回的邮件地址，不进行删除
    for (int i=0; i<[allListArray count]; i++) 
    {
        NSMutableDictionary *listCellDic = [allListArray objectAtIndex:i];
        
        NSString* listCellTag = [listCellDic objectForKey:@"cellTag"];
        
        //遍历所有的list，取得所有选择的shareid
        NSMutableArray *tagArray = [[NSMutableArray alloc] initWithArray:m_checkedArray];
        for (NSMutableDictionary* tagDic in tagArray)
        {
            bIsExist = NO;
            NSString* strCellTag = [tagDic objectForKey:@"cellTag"];
            if ([listCellTag compare:strCellTag] == 0) 
            {
                NSString* strSendAddr = [listCellDic objectForKey:@"sendaddress"];
                NSString* sendAddr    =  [self getAddress:strSendAddr];
                for (NSString* strMergeAddr in arrayMerge)
                {
                    if ([strMergeAddr compare:sendAddr] == 0) 
                    {
                        nNotMergeCount++;
                        bIsExist = YES;
                        break;
                    }
                    
                }
                if (bIsExist == NO) 
                {
                    [tagArray removeObject:tagDic];
                    [allListArray removeObject:listCellDic];
                    i--;
                }
                break;
            }
        }
    }
    
    //如果有没有归并的邮件弹出提示
    if (nNotMergeCount > 0)
    {
        //[m_checkedArray removeAllObjects];
        NSString* strToast = [NSString stringWithFormat:@"您有%i封邮件，无法归并",nNotMergeCount];
        [myToast showWithText:strToast];
    }else
    {
        NSString* strToast = @"全部归并成功";
        [myToast showWithText:strToast];
    }
    
   [m_checkedArray removeAllObjects];
    [xml release];
    
    //删除后，浮动条隐藏
    [self onHide];
    [self.m_table reloadData];
   
}

//******************************************************
// Method:     getMailAddress
// Access:     public 
// Returns:    NSString
// Parameter:  originMailAddr
// Note:	   去掉 < > 提取邮件地址
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(NSString*) getAddress:(NSString*) originMailAddr
{
    NSString* temp    = originMailAddr;
    NSString *strSplit = temp;//@"<m>missed call</m>";
    NSRange rsrange;
    rsrange.length   = 0;
    rsrange.location = 0;//index是在头文件中申请好的全局变量.int型 初始值为0
    NSRange xrange = [strSplit rangeOfString:@"<"];
    if (xrange.length ==0)
    {
        return @"";
    }
    int x = xrange.location;
    
    NSRange yrange = [strSplit rangeOfString:@">"];
    
    int y = yrange.location;
    NSString *result = [strSplit substringFromIndex:x+1];
    result = [result substringToIndex:y-x-1];
    return result;
}
//******************************************************
// Method:     onHide
// Access:     public 
// Returns:    void
// Parameter:  int
// Note:	    隐藏邮件
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void) onHide
{
    [m_checkedArray removeAllObjects];
     m_toolDrawerView.hidden = YES;
    [self.m_table reloadData];
}

//******************************************************
// Method:     onShow
// Access:     public 
// Returns:    void
// Parameter:  int
// Note:	   显示邮件
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void) onShow
{
    //取得 NavigationMainViewController 的指针
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NavigationMainViewController* mainView = [appDelegate getMainViewCtl];
    
    if(mainView.m_bStafferClient != NO)
    {
        switch (mainView.m_boxType) 
        {
            case RECEIVE_BOX: 
            case HAVE_SENDED_BOX: 
            {
                [m_mergeBut setEnabled:YES]; 
                break;
            }
            default:
            {
                [m_mergeBut setEnabled:NO];   
                break;
            }
        }
        m_toolDrawerView.hidden = NO;
    }
    
}

//******************************************************
// Method:     onWriteMail
// Access:     public 
// Returns:    void
// Parameter:  int
// Note:	   撰写邮件按钮事件
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void) onWriteMail
{
    MailMessageComposer *msgC = [[MailMessageComposer alloc] initWithNibName:
                                 @"MailMessageComposer" bundle:nil];
    msgC.m_mailComType = NEW_COMPOSER;
    //跳转页面后，禁止滑动事件
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NavigationMainViewController* mainView = [appDelegate getMainViewCtl];
    mainView.m_nav.motionDelegate = nil;
    
    [self.navigationController setNavigationBarHidden:YES];
    [self.navigationController pushViewController:msgC animated:YES];
    [msgC release];
    //[self.view.superview removeFromSuperview];
    //[self.view insertSubview:msgC.view atIndex:0];
    
    
    //	navBar = [[UINavigationController alloc] initWithRootViewController:msgC];
    //	[navBar setNavigationBarHidden:YES];
    //	[window addSubview:navBar.view];
    //	[window makeKeyAndVisible];
    NSLog(@"onWriteMail");
}

//******************************************************
// Method:     OnSearchMail
// Access:     public 
// Returns:    void
// Parameter:  int
// Note:	   查找邮件
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void)OnSearchMail
{
    /*
     遍历当前searchbar中的控件，找到cancel按钮，并修改为中文
     */
    m_mailSearchBar.showsCancelButton = YES;
    for(id ctl in [m_mailSearchBar subviews])
    {
        if([ctl isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)ctl;
            [btn setTitle:@"取消"  forState:UIControlStateNormal];
        }
    }
    //    NSLog(@"find");
    [m_searchDisplayController.searchBar setSelectedScopeButtonIndex:0];
    [m_searchDisplayController setActive:YES animated:YES];
}

//******************************************************
// Method:     onNavigation
// Access:     public 
// Returns:    void
// Parameter:  int
// Note:	   导航栏按钮事件
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void) onNavigation
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NavigationMainViewController *mainView = [appDelegate getMainViewCtl];
    [mainView chickSplitHalfView];
    
    //切换为半屏的时候，隐藏浮动窗口
    [self onHide];
}

//******************************************************
// Method:     makeLeftViewVisible
// Access:     public 
// Returns:    void
// Parameter:  int
// Note:	   左边的窗口可见
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)makeLeftViewVisible 
{
  
}

//******************************************************
// Method:     viewDidLoad
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   右边的窗口不可见
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)makeRightViewVisible 
{
    
}

//******************************************************
// Method:     viewDidLoad
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   初始化后第一次调用
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self init];

    //默认是不进行搜索
    m_bIsSearching = NO;
    
    //最上面的bar
    m_navBar=[[UINavigationBar alloc]init];
    m_navBar.frame = CGRectMake(0, 0, 320, 44);
    
    //设置导航栏带阴影
    CALayer *sublayer = self.navigationController.view.layer;
    sublayer.masksToBounds = NO;
    sublayer.shadowColor = [UIColor blackColor].CGColor;
    sublayer.shadowOffset = CGSizeMake(-10, 0.0);
    sublayer.shadowOpacity = 0.25;
    //sublayer.borderWidth  =-3.0; 
    //sublayer.shadowRadius = 3.0;
    //sublayer.shouldRasterize = YES;
    
    //TOP BAR 左边的导航按钮
    UIButton* barNaviBut = [[UIButton alloc] initWithFrame:CGRectMake(8.0, 7.0, 45.0, 33.0)];
    UIImage *barNaviButImageNormal = [UIImage imageNamed:PIC_MAIL_HALF_SCREEN_PRESS];
    [barNaviBut setBackgroundImage:barNaviButImageNormal forState:UIControlStateNormal];

    UIImage *barNaviButImagePressed = [UIImage imageNamed:PIC_MAIL_HALF_SCREEN_NORMAL];
    [barNaviBut setBackgroundImage:barNaviButImagePressed forState:UIControlStateHighlighted];
    
    [barNaviBut setTitle:@"" forState:UIControlStateNormal];
    [barNaviBut addTarget:self action:@selector(onNavigation) forControlEvents:UIControlEventTouchUpInside];
    
    //邮箱title
    m_barTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(125.0, 5.0, 100.0, 32.0)];
    [m_barTitleLabel setBackgroundColor:[UIColor  clearColor]];
    m_barTitleLabel.textColor = [UIColor whiteColor];
    m_barTitleLabel.text = @"收件箱";

    //搜索按钮
    UIButton* barSearchBut = [[UIButton alloc] initWithFrame:CGRectMake(220.0, 0.0, 55.5, 44.0)];
    UIImage *barSearchImageNormal = [UIImage imageNamed:PIC_MAIL_SEARCH_NORMAL];
    UIImage *barSearchImagePress = [UIImage imageNamed:PIC_MAIL_SEARCH_PRESS];
    [barSearchBut setBackgroundImage:barSearchImageNormal forState:UIControlStateNormal];
    [barSearchBut setBackgroundImage:barSearchImagePress forState:UIControlStateHighlighted];
    [barSearchBut addTarget:self action:@selector(OnSearchMail) forControlEvents:UIControlEventTouchUpInside];
    
    //撰写按钮
    UIButton* barWriteBut = [[UIButton alloc] initWithFrame:CGRectMake(265.0, 0.0, 55.5, 44.0)];
    UIImage *barWriteImageNormal = [UIImage imageNamed:PIC_MAIL_WRITE_NORMAL];
    UIImage *barWriteImagePress = [UIImage imageNamed:PIC_MAIL_WRITE_PRESS];
    [barWriteBut setBackgroundImage:barWriteImageNormal forState:UIControlStateNormal];
    [barWriteBut setBackgroundImage:barWriteImagePress forState:UIControlStateHighlighted];
    [barWriteBut addTarget:self action:@selector(onWriteMail) forControlEvents:UIControlEventTouchUpInside];

    [m_navBar addSubview:barWriteBut];
    [m_navBar addSubview:barSearchBut];
    [m_navBar addSubview:barNaviBut];
    [m_navBar addSubview:m_barTitleLabel];

    //设置导航栏背景颜色
    
    UIImage* imageBar = [UIImage imageNamed:PIC_MAIL_TOP_BAR];
    [self.navigationController setNavigationBarHidden:NO];

    NSString* devVersion = [[UIDevice currentDevice] systemVersion];
    if ([devVersion compare:@"4.3.3"] == 0)
    {
        [self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)];
          [self.navigationController.navigationBar setTintColor:NAVIGATION_BAR_COLOR];
    }else
    {
        [self.navigationController.navigationBar setBackgroundImage:imageBar forBarMetrics:UIBarMetricsDefault];
    }
    self.m_mailSearchBar.showsCancelButton = NO;
    
    //弹出bar
	UIButton *button;
    m_toolDrawerView = [[ToolDrawerView alloc]initInVerticalCorner:kBottomCorner andHorizontalCorner:kLeftCorner moving:kHorizontally];
    [m_toolDrawerView SetSize:47 height:36];
    [m_toolDrawerView SetCtlPos:CGPointMake(318, 49)];
     AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.viewController.view addSubview:m_toolDrawerView];
    
    UIImage* imageView = [UIImage imageNamed:PIC_MAIL_FLOAT_TOOLBAR_BG];
    UIImageView* personImage = [[UIImageView alloc]initWithFrame:CGRectMake(-10, 0, 202, 44)];
    [personImage setImage:imageView];
    [m_toolDrawerView addSubview:personImage];
    
    //创建显示统计邮件数量
    m_mailNumTotal = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
    [m_mailNumTotal setTitle:@"5" forState:UIControlStateNormal];
    [m_mailNumTotal setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    [m_toolDrawerView appendButton:m_mailNumTotal];
    
    //删除按钮
	button = [UIButton buttonWithType:UIButtonTypeCustom];
	[button setTitle:@"" forState:UIControlStateNormal];
    
    //设置按钮上的自体的大小
    button.titleLabel.font = [UIFont systemFontOfSize: 13.0];
    UIImage *buttonImageNormal = [UIImage imageNamed:PIC_MAIL_FLOAT_TOOLBAR_DELETE_NORMAL];
    [button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
    UIImage *buttonImagePressed = [UIImage imageNamed:PIC_MAIL_FLOAT_TOOLBAR_DELETE_PRESS];
    [button setBackgroundImage:buttonImagePressed forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(onDelete) forControlEvents:UIControlEventTouchUpInside];  //设置按钮点击事件   
	[m_toolDrawerView appendButton:button];
    //[button release];
    
    //归并按钮
	m_mergeBut = [UIButton buttonWithType:UIButtonTypeCustom];
	[m_mergeBut setTitle:@"" forState:UIControlStateNormal];
    
    //设置按钮上的自体的大小
    buttonImageNormal = [UIImage imageNamed:PIC_MAIL_FLOAT_TOOLBAR_MERGE_NORMAL];
    buttonImagePressed = [UIImage imageNamed:PIC_MAIL_FLOAT_TOOLBAR_MERGE_PRESS];
    m_mergeBut.titleLabel.font = [UIFont systemFontOfSize: 13.0];
    [m_mergeBut setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
    [m_mergeBut setBackgroundImage:buttonImagePressed forState:UIControlStateHighlighted];
    [m_mergeBut addTarget:self action:@selector(onMerge) forControlEvents:UIControlEventTouchUpInside];  //设置按钮点击事件  
	[m_toolDrawerView appendButton:m_mergeBut];
    //[button release];
    
    //隐藏按钮
	button = [UIButton buttonWithType:UIButtonTypeCustom];
	[button setTitle:@"" forState:UIControlStateNormal];
    
    //设置按钮上的自体的大小
    button.titleLabel.font = [UIFont systemFontOfSize: 13.0];
    buttonImageNormal = [UIImage imageNamed:PIC_MAIL_FLOAT_TOOLBAR_CLOSE_NORMAL];
    [button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
    buttonImagePressed = [UIImage imageNamed:PIC_MAIL_FLOAT_TOOLBAR_CLOSE_PRESS];
    [button setBackgroundImage:buttonImagePressed forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(onHide) forControlEvents:UIControlEventTouchUpInside];  //设置按钮点击事件  
	[m_toolDrawerView appendButton:button];
    //[button release];

   //[m_toolDrawerView setBackgroundColor: [UIColor colorWithPatternImage: [UIImage imageNamed: @"src"]]];
    //隐藏弹出菜单
    m_toolDrawerView.hidden = YES;
    
    //search bar
    m_mailSearchBar = [[UISearchBar alloc] init];
    [m_mailSearchBar setScopeButtonTitles:[NSArray arrayWithObjects:@"邮件标题",@"发件人",nil]];
    m_mailSearchBar.delegate = (id)self;
    [m_mailSearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    [m_mailSearchBar sizeToFit];
    UITextField *searchField = [[m_mailSearchBar subviews] lastObject];
    [searchField setReturnKeyType:UIReturnKeyDone];
    self.tableView.tableHeaderView = m_mailSearchBar;
    [m_mailSearchBar setTintColor:NAVIGATION_BAR_COLOR];
    
    //self.tableView.tableHeaderView.hidden = YES;
    [m_mailSearchBar release];
    
#ifdef _OLD_NAVIGATION_CREATE
    //导航栏最右边的撰写按钮
    //查找和撰写
    UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 75, 44)];
    
    [tools setBackgroundImage:imageBar forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    [tools setTintColor:[self.navigationController.navigationBar tintColor]]; 
    [tools setAlpha:[self.navigationController.navigationBar alpha]]; 
    NSMutableArray* butArray = [[NSMutableArray alloc] initWithCapacity:2];
    UIBarButtonItem *searchBut = [[UIBarButtonItem alloc] 
                                          initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
                                          target:self 
                                          action:@selector(OnSearchMail)];
        
    UIBarButtonItem *writeMailBut = [[UIBarButtonItem alloc] 
                                           initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks
                                           target:self 
                                           action:@selector(onWriteMail)]; 
        
    [butArray addObject:searchBut]; 
    [searchBut release]; 
    [butArray addObject:writeMailBut]; 
    [writeMailBut release]; 
    [tools setItems:butArray animated:NO]; 
    [butArray release]; 
    UIBarButtonItem *butItem = [[UIBarButtonItem alloc] initWithCustomView:tools]; 
    //self.navigationItem.rightBarButtonItem = butItem;

    [butItem release]; 
    [tools release];

    //导航栏最左边的导航按钮
    UIToolbar* leftTools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 75, 44)];
    
    [leftTools setBackgroundImage:imageBar forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    [leftTools setTintColor:[self.navigationController.navigationBar tintColor]]; 
    [leftTools setAlpha:[self.navigationController.navigationBar alpha]]; 
    NSMutableArray* navArray = [[NSMutableArray alloc] initWithCapacity:1];
    
    UIImage * imageNav = [UIImage imageNamed:@"gear_32"];

    UIBarButtonItem *navBut = [[UIBarButtonItem alloc]initWithImage:imageNav style:UIBarButtonItemStylePlain target:self action:@selector(onNavigation)];

    [navBut setImage:imageNav];
    [navArray addObject:navBut];  
    [navBut release]; 
    [leftTools setItems:navArray animated:NO]; 
    [navArray release]; 
    UIBarButtonItem *navItem = [[UIBarButtonItem alloc] initWithCustomView:leftTools]; 
    //self.navigationItem.leftBarButtonItem = navItem;
    [navItem release]; 
    [leftTools release];
    self.tableView.backgroundColor = BACKGROUND_COLOR;//[UIColor whiteColor];
#endif  
    
    /*
     添加searchbar到searchDisplayController，并设置
     代理代理方法
    */
    m_searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:m_mailSearchBar contentsController:self];
    [m_searchDisplayController setDelegate:(id)self];
    [m_searchDisplayController setSearchResultsDataSource:self];
    [m_searchDisplayController setActive:NO];
    [self.m_table setContentOffset:CGPointMake(0, 44.f) animated:NO];
    [m_mailSearchBar release];
    
  

    [self.navigationController.navigationBar addSubview:barWriteBut];
    [self.navigationController.navigationBar addSubview:barSearchBut];
    [self.navigationController.navigationBar addSubview:barNaviBut];
    [self.navigationController.navigationBar addSubview:m_barTitleLabel];
    
    [barWriteBut release];
    [barSearchBut release];
    [barNaviBut release];
    
    self.tableView.backgroundColor = BACKGROUND_COLOR;
    
    //第一次登陆的时候，保存业务员id
    m_employeeID = [[NSString alloc] initWithFormat:@"%@" ,[[Session share] getMemberID]];
    
    //开始菊花转动
    m_spinner = [[TKLoadingAnimationView alloc] initWithFrame:CGRectMake(140, 155, 30, 30) 
                                  tkLoadingAnimationViewStyle:TKLoadingAnimationViewStyleCarton];
    
	[self.view addSubview:m_spinner];
	[m_spinner startAnimating];
	[m_spinner release];
}
//******************************************************
// Method:     textFieldShouldReturn
// Access:     public 
// Returns:    void
// Parameter:  void
// Note:	   搜索，返回键盘响应事件
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

//******************************************************
// Method:     writeMail
// Access:     public 
// Returns:    void
// Parameter:  void
// Note:	   撰写邮件
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void)writeMail
{
    MailMessageComposer *msgC = [[MailMessageComposer alloc] initWithNibName:
                                 @"MailMessageComposer" bundle:nil];
    
    //跳转页面后，禁止滑动事件
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NavigationMainViewController* mainView = [appDelegate getMainViewCtl];
    mainView.m_nav.motionDelegate = nil;
    
    [self.navigationController setNavigationBarHidden:YES];
    [self.navigationController pushViewController:msgC animated:YES];
    [msgC release];
    
    NSLog(@"add");
}

//******************************************************
// Method:     viewDidUnload
// Access:     public 
// Returns:    void
// Parameter:  void
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
     [[[TaskManager Share] GetTaskByType:GetMailList Delegate:self] cancel];
}

//******************************************************
// Method:     viewDidUnload
// Access:     public 
// Returns:    void
// Parameter:  void
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO];
    //Hide the search bar
	[self.tableView setContentOffset:CGPointMake(0, 44.f) animated:NO];

    if ([m_checkedArray count] > 0) 
    {
        [self onShow];
    }
    [super viewWillAppear:animated];
    
    [self refreshData];
}

//******************************************************
// Method:     viewDidUnload
// Access:     public 
// Returns:    void
// Parameter:  void
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)viewDidAppear:(BOOL)animated
{
    //重新进行滑动事件
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NavigationMainViewController* mainView = [appDelegate getMainViewCtl];
    mainView.m_nav.motionDelegate = mainView;
    [super viewDidAppear:animated];
    self.m_mailSearchBar.showsCancelButton=NO;
    
    //[self refreshData];

}

//******************************************************
// Method:     viewDidUnload
// Access:     public 
// Returns:    void
// Parameter:  void
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)viewWillDisappear:(BOOL)animated
{
    [self onHide];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

//******************************************************
// Method:     tableView
// Access:     public 
// Returns:    void
// Parameter:  tableView
// Parameter:  indexPath
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 73;
}

//******************************************************
// Method:     numberOfSectionsInTableView
// Access:     public 
// Returns:    void
// Parameter:  tableView
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

//******************************************************
// Method:     numberOfSectionsInTableView
// Access:     public 
// Returns:    void
// Parameter:  tableView
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [m_mutableArray count];
}

//******************************************************
// Method:     tableView
// Access:     public 
// Returns:    void
// Parameter:  tableView
// Parameter:  indexPath
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取得行数
    int row = indexPath.row;
    NSString* strTag = [NSString  stringWithFormat:@"%d",row]; 
    
    //取得cellview的对象
    NSString *CellIdentifier = [NSString stringWithFormat:@"MailListCell",indexPath.row];
    MailListCell* cell = (MailListCell*)[[self tableView] dequeueReusableCellWithIdentifier:CellIdentifier];
    
    //如果不成功，从xib文件中重新取得
    if(cell == nil)
    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"MailListCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        //cell.accessibilityIdentifier = CellIdentifier;
    }
    if ([m_mutableArray count]==0)
    {
        return cell;
    }
    //同步设置mutableArray中的tag
    NSMutableDictionary * listDic;
    if (row < [m_mutableArray count]) 
    {
        listDic = [m_mutableArray objectAtIndex:row];
    }else
    {
        listDic = [[NSMutableDictionary alloc]initWithCapacity:0];
    }
    
    [listDic setObject:strTag forKey:@"cellTag"];
    //在cellview中添加背景view
    UIView *view = [[UIView alloc] init];
    cell.backgroundView = view;
    [view release];
    
    //给当前的cell添加tag
    cell.tag = indexPath.row;
    
    //添加checkbox的图片，因为在界面重新刷新后，背景图片会被清除。我们自己重新绘制一下
    NSMutableArray *tmpArray = m_checkedArray;
    cell.isChecked = NO;
    [cell.mailCheckBox setImage:[UIImage imageNamed:PIC_MAIL_NO_SELECT] forState:UIControlStateNormal];
    for (NSMutableDictionary* tagDic in tmpArray)
    {
        NSString* strCellTag = [tagDic objectForKey:@"cellTag"];
        if ([strCellTag compare:strTag] == 0) 
        {
            cell.backgroundView.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0];
             
            [cell.mailCheckBox setImage:[UIImage imageNamed:PIC_MAIL_SELECT] forState:UIControlStateNormal];
            
            break;
        }
    }
    //添加是否重要邮件标志的图片，因为在界面重新刷新后，背景图片会被清除。我们自己重新绘制一下
    
    [cell.mailImportantFlag setImage:[UIImage imageNamed:PIC_MAIL_NO_IMPORTANCE] forState:UIControlStateNormal];
    //设置重要性标志位
    NSString* importFlg = [listDic objectForKey:@"importantflag"];
    
    NSLog(@"strtag:---%@---importantflag--%@",strTag,importFlg);
    
    if ([importFlg compare:@"1"] == 0) 
    {
        [self setImportFlag:strTag];
    }
    
    NSMutableArray *array = m_importantArray;
    NSLog(@"m_importantArraycount=: %i",[m_importantArray count]);
    for (NSMutableDictionary* tagDic in array)
    {
        NSString* strCellTag = [tagDic objectForKey:@"cellTag"];
        if ([strCellTag compare:strTag] == 0) 
        {
            NSLog(@"StarBlue.png:: strtag:---%@---importantflag--%@",strTag,importFlg);
            [cell.mailImportantFlag setImage:[UIImage imageNamed:PIC_MAIL_IMPORTANCE] forState:UIControlStateNormal];
            break;
        }
    }
    //添加数据到cellview中
    cell._mailList = self;
    cell.mailsubject.text = [listDic objectForKey:@"subject"];
    cell.mailaddress.text = [listDic objectForKey:@"sendaddress"];
    cell.mailtime.text    = [listDic objectForKey:@"receivesendtime"];
    NSString* context_text = [listDic objectForKey:@"context_txt"];
    cell.mailContent.text = context_text;
    
    NSString* strReadFlag = [listDic objectForKey:@"readflag"];
    
    //取得是否已经读的标志
    if ( [strReadFlag compare:@"0"] == 0 ) 
    {
        cell.mailsubject.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:18];
        cell.mailaddress.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:18];
        cell.mailtime.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:18];   
        cell.mailContent.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:18];
        cell.mailsubject.textColor = [UIColor blueColor];
        cell.mailaddress.textColor = [UIColor blueColor];
        cell.mailtime.textColor = [UIColor blueColor]; 
        cell.mailContent.textColor = [UIColor blueColor];
    }else
    {
        cell.mailsubject.font = [UIFont fontWithName:@"Arial" size:15];
        cell.mailaddress.font = [UIFont fontWithName:@"Arial" size:15];
        cell.mailtime.font = [UIFont fontWithName:@"Arial" size:15];   
        cell.mailContent.font = [UIFont fontWithName:@"Arial" size:15];
        cell.mailsubject.textColor = [UIColor blackColor];
        cell.mailaddress.textColor = [UIColor blackColor];
        cell.mailtime.textColor = [UIColor blackColor]; 
        cell.mailContent.textColor = [UIColor blackColor];
    }
    
    //是否有附件
    NSString* filesize =  [listDic objectForKey:@"filesize"];
    if ([filesize compare:@""] != 0)
    {
        UIImage* attachImage = [UIImage imageNamed:PIC_MAIL_ACCESSORY];
        [cell.mailAttachImage setImage:attachImage];
    }else
    {
        [cell.mailAttachImage setImage:nil];
    }
    
    return cell;
}
#pragma mark －－－－－－MailListCell上的控件的操作－－－－－－－－－－

//******************************************************
// Method:     setImportFlag
// Access:     public 
// Returns:    void
// Parameter:  strTag
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void) setImportFlag:(NSString*)strTag
{
    BOOL bFlag = TRUE;
    NSMutableArray *temArray = m_importantArray;
    NSMutableDictionary *temDictionary = [[NSMutableDictionary alloc] initWithCapacity:0];
    for (NSMutableDictionary* tagDic in temArray)
    {
        NSString* strCellTag = [tagDic objectForKey:@"cellTag"];
        if ([strCellTag compare:strTag]==0)
        {
            bFlag = FALSE;
            break;
        }
    }
    if (bFlag) 
    {
        [temDictionary setObject:strTag forKey:@"cellTag"];                                        
        [m_importantArray addObject:temDictionary];
    }
    [temDictionary release];
}
#pragma mark - Table view delegate

//******************************************************
// Method:     tableView
// Access:     public 
// Returns:    void
// Parameter:  tableView
// Parameter:  didSelectRowAtIndexPath
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"[MailList]-[didSelectRowAtIndexPath]---start");
    
    //单击后，改变查询状态。
    m_bIsSearching = NO;
    //跳转页面后，禁止滑动事件
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NavigationMainViewController* mainView = [appDelegate getMainViewCtl];
    mainView.m_nav.motionDelegate = nil;
    //MailListCell *cell = (MailListCell*)[self.tableView cellForRowAtIndexPath:indexPath];
    
    //设置已经读的标志位
    int count = [m_mutableArray count];
    if (count == 0)
    {
        return;
    }
    
    NSMutableDictionary * listDic;
    if (indexPath.row < [m_mutableArray count]) 
    {
        listDic = [m_mutableArray objectAtIndex:indexPath.row];
    }else
    {
        listDic = [[NSMutableDictionary alloc]initWithCapacity:0];
    }
    
    NSString* strShareID = @"";
    NSString* strMailID  = @"";
    
    if(mainView.m_bStafferClient == NO)
    {
        NSString* strTransferFlag = [listDic objectForKey:@"transferflag"];
       if ([strTransferFlag compare:@"1"] == 0)
       {
           strMailID = [listDic objectForKey:@"mailid"];
       }
       else
       {
           strShareID =  [listDic objectForKey:@"id"];
       }
      
    }
    else
    {
       strShareID =  [listDic objectForKey:@"id"];
    }
    
    NSString* strReadFlag = [listDic objectForKey:@"readflag"];
    if ( [strReadFlag compare:@"0"] == 0 )
    {
        XmlRpc *xml = [[XmlRpc alloc] init];
        [xml settingMailReadFlag:strShareID]; 
        [listDic setValue:@"1" forKey:@"readflag"];
        [xml release];
    }
    //push 到下一个页面
    MailDetailMessage *mailDetailMsg = [[MailDetailMessage alloc] init];
    
    //保存当前邮件的shareid和行数
    [mailDetailMsg SetMailShareID:strShareID mailID:strMailID rowIndex:indexPath.row boxType:mainView.m_boxType];
    
    //保存maillist指针
    mailDetailMsg.m_mailList = self;
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self.navigationController pushViewController:mailDetailMsg animated:YES];
    [mailDetailMsg release];
    NSLog(@"[MailList]-[didSelectRowAtIndexPath]---end");
}


#pragma mark - my func
//******************************************************
// Method:     refreshData
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void)refreshData
{
    NSLog(@"refresh data");
    [[[TaskManager Share] GetTaskByType:GetMailList Delegate:self] Excute]; 
}

//******************************************************
// Method:     getMoreData
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void) getMoreData
{
    NSLog(@"more data");
}

//******************************************************
// Method:     OnPreExcute
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void) OnPreExcute
{
    [super OnPreExcute];
}

//******************************************************
// Method:     DoInBackground
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void) DoInBackground
{
    /*
     清空以前的数据,拖拽数据和正常点击的数据分开，
     正常点击，重新获得数据库的数据，拖拽是增加
     数据
     */
    int nBoxNum          = 0;
    NSString * strBoxNum = @"";
    
    if (m_refreshType == TRIGGER_FRESH)  
    {
        m_refreshType = NORMAL_FRESH;
    }else
    {
        [m_mutableArray removeAllObjects];
        [m_importantArray removeAllObjects];
    }
    //保存来自服务器中的数据
    NSMutableArray *mailListArray = nil;
    
    m_startRow            = [m_mutableArray count];
    NSString *num         = [NSString stringWithFormat: @"%i",m_startRow];
    NSString *rowsperpage = [NSString stringWithFormat:@"%d",PER_PAGE_ROWS];
    NSMutableArray *tmp   = nil;
    //m_employeeID          = [[NSString alloc] initWithFormat:@"%@" ,[[Session share] getMemberID]];
    XmlRpc *xml           = [[XmlRpc alloc] init];
    
    //取得 NavigationMainViewController 的指针
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NavigationMainViewController* mainView = [appDelegate getMainViewCtl];
    
    if(mainView.m_bStafferClient == NO)
    {
        switch (mainView.m_boxType) 
        {
            case RECEIVE_BOX: 
            {
                if (m_bIsSearching) 
                {
                    tmp = [xml getSearchCustomerMailList:m_employeeID customer_id:m_selectClientID boxType:@"01" search_condition:m_searchText search_type:m_searchScopeType startRows:num rowsPerPage:rowsperpage];
                } 
                else
                {
                    //取得未读邮件的数量
                    if ([mainView.m_ClientSwittchName compare:@""] ==0) 
                    {
                        [xml getFirstCustomerMailList:m_employeeID positionID:@"" boxType:@"01" startRows:num rowsPerPage:rowsperpage];
                    }else
                    {
                        tmp = [xml getCustomerMailList:m_employeeID  customer_id:m_selectClientID box:@"01"startRows:num rowsPerPage:rowsperpage];                  
                    }
                    tmp = [xml getCustomerMailList:m_employeeID  customer_id:m_selectClientID box:@"01"startRows:num rowsPerPage:rowsperpage]; 
                } 
                mailListArray = [[NSMutableArray alloc] initWithArray:tmp];
                break;
            }
            case SEND_BOX:
            {
                if (m_bIsSearching) 
                {
                    tmp = [xml getSearchCustomerMailList:m_employeeID customer_id:m_selectClientID boxType:@"02" search_condition:m_searchText search_type:m_searchScopeType startRows:num rowsPerPage:rowsperpage];
                }
                else
                {
                    if ([mainView.m_ClientSwittchName compare:@""] ==0) 
                    {
                        [xml getFirstCustomerMailList:m_employeeID positionID:@"" boxType:@"02" startRows:num rowsPerPage:rowsperpage];
                    }
                    else
                    {
                        tmp = [xml getCustomerMailList:m_employeeID customer_id:m_selectClientID box:@"02" startRows:num rowsPerPage:rowsperpage]; 
                    }
                    
                } 
                mailListArray = [[NSMutableArray alloc] initWithArray:tmp];
                break;
            }
            default:
                break;
        }
        
    }
    else
    {
        switch (mainView.m_boxType) 
        {
            case RECEIVE_BOX:       //接收邮箱
            {
                if (m_bIsSearching) 
                {
                    tmp = [xml getSearchMailList:m_employeeID boxType:@"01" search_condition:m_searchText search_type:m_searchScopeType startRows:num rowsPerPage:rowsperpage];
                }
                else
                {
                    //取得未读邮件的数量
                    nBoxNum   = [xml getMailByBoxCountNoRead:m_employeeID boxType:@"01"]; 
                    strBoxNum = [NSString stringWithFormat:@"%i",nBoxNum];
                    
                    //设置未读邮件的数量
                    if (nBoxNum > 0) 
                    {
                        [mainView.m_receiveMailNum setHidden:NO];
                        [mainView.m_receiveMailNum setText:strBoxNum];
                        m_barTitleLabel.text = [NSString stringWithFormat:@"收件箱(%@)", strBoxNum];
                    }
                    else
                    {
                        [mainView.m_receiveMailNum setHidden:YES];
                        m_barTitleLabel.text = @"收件箱";
                    }
                    
                    tmp       = [xml getMailList:m_employeeID box:@"01" startRows:num rowsPerPage:rowsperpage]; 
                } 
                
                mailListArray = [[NSMutableArray alloc] initWithArray:tmp];
                break;
            }
                
            case WAIT_SEND_BOX:          //发送邮箱
            {
                if (m_bIsSearching) 
                {
                    tmp = [xml getSearchMailList:m_employeeID boxType:@"021" search_condition:m_searchText search_type:m_searchScopeType startRows:num rowsPerPage:rowsperpage];
                }
                else
                {
                    tmp = [xml getMailList:m_employeeID box:@"021" startRows:num rowsPerPage:rowsperpage]; 
                }
                mailListArray = [[NSMutableArray alloc] initWithArray:tmp];
                break;
            }
            case WAIT_EVALUTE_BOX:          //发送邮箱
            {
                if (m_bIsSearching) 
                {
                    tmp = [xml getSearchMailList:m_employeeID boxType:@"022" search_condition:m_searchText search_type:m_searchScopeType startRows:num rowsPerPage:rowsperpage];
                }
                else
                {
                    tmp = [xml getMailList:m_employeeID box:@"022" startRows:num rowsPerPage:rowsperpage]; 
                }
                mailListArray = [[NSMutableArray alloc] initWithArray:tmp];
                break;
            }
            case WITHDRAWL_BOX:          //发送邮箱
            {
                if (m_bIsSearching) 
                {
                    tmp = [xml getSearchMailList:m_employeeID boxType:@"024" search_condition:m_searchText search_type:m_searchScopeType startRows:num rowsPerPage:rowsperpage];
                }
                else
                {
                    tmp = [xml getMailList:m_employeeID box:@"024" startRows:num rowsPerPage:rowsperpage]; 
                }
                mailListArray = [[NSMutableArray alloc] initWithArray:tmp];
                break;
            }
            case HAVE_SENDED_BOX:   //已发送邮箱
            {
                if (m_bIsSearching) 
                {
                    tmp = [xml getSearchMailList:m_employeeID boxType:@"03" search_condition:m_searchText search_type:m_searchScopeType startRows:num rowsPerPage:rowsperpage];
                }
                else
                {
                    tmp = [xml getMailList:m_employeeID box:@"03" startRows:num rowsPerPage:rowsperpage];
                }
                mailListArray = [[NSMutableArray alloc] initWithArray:tmp];
                break;
            }
            case DRAFT_BOX:         //草稿邮箱
            {
                if (m_bIsSearching) 
                {
                    tmp = [xml getSearchMailList:m_employeeID boxType:@"04" search_condition:m_searchText search_type:m_searchScopeType startRows:num rowsPerPage:rowsperpage];
                }
                else
                {
                    tmp = [xml getMailList:m_employeeID box:@"04" startRows:num rowsPerPage:rowsperpage]; 
                }
                mailListArray = [[NSMutableArray alloc] initWithArray:tmp];
                break;
            }
            case TRASH_BOX:        //垃圾邮箱
            {
                if (m_bIsSearching) 
                {
                    tmp = [xml getSearchMailList:m_employeeID boxType:@"05" search_condition:m_searchText search_type:m_searchScopeType startRows:num rowsPerPage:rowsperpage];
                }
                else
                {
                    tmp = [xml getMailList:m_employeeID box:@"05" startRows:num rowsPerPage:rowsperpage]; 
                }
                mailListArray = [[NSMutableArray alloc] initWithArray:tmp];
                break;
            }
            case RECYCLE_BOX:       //回收箱
            {
                if (m_bIsSearching) 
                {
                    tmp = [xml getSearchMailList:m_employeeID boxType:@"06" search_condition:m_searchText search_type:m_searchScopeType startRows:num rowsPerPage:rowsperpage];
                }
                else
                {
                    tmp = [xml getMailList:m_employeeID box:@"06" startRows:num rowsPerPage:rowsperpage]; 
                }
                mailListArray = [[NSMutableArray alloc] initWithArray:tmp];
                break;
            }
            case DELETED_BOX:       //回收箱
            {
                if (m_bIsSearching) 
                {
                    tmp = [xml getSearchMailList:m_employeeID boxType:@"07" search_condition:m_searchText search_type:m_searchScopeType startRows:num rowsPerPage:rowsperpage];
                }
                else
                {
                    tmp = [xml getMailList:m_employeeID box:@"07" startRows:num rowsPerPage:rowsperpage];
                }
                mailListArray = [[NSMutableArray alloc] initWithArray:tmp];
                break;
            }
            default:
                break;
        }
    }

    
    int i = 0;
    for (NSMutableDictionary* mailDic in mailListArray)
    {
        //取得行数
        int row = m_startRow + i;
        NSString* strTag = [NSString  stringWithFormat:@"%d",row];
       [mailDic setObject:strTag forKey:@"cellTag"];
        NSString* importFlg = [mailDic objectForKey:@"importantflag"];
        if ([importFlg compare:@"1"] == 0) 
        {
            [m_importantArray addObject:mailDic];
            NSLog(@"importFlg:%i,%@",i,importFlg);
        }
        i++;
        NSString* strSendAddr  = [mailDic objectForKey:@"sendaddress"];
        
        [mailDic setValue:strSendAddr forKey:@"sendaddress"];
        
        [m_mutableArray addObject:mailDic];
    }
    [mailListArray release];
    [xml release];
    

}

-(void)GetMailAddress:(NSString*)strAddr
{
    
}

//******************************************************
// Method:     OnPostExcute
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void) OnPostExcute
{
    //停止菊花转动
    [m_spinner stopAnimating];
    
    [super OnPostExcute];
    [self.m_table reloadData];
}

//******************************************************
// Method:     OnPostExcute
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	    向下拖拽刷新
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)egoRefreshTableFooterDidTriggerRefresh:(EGORefreshTableFooterView*)view
{
    m_refreshType = TRIGGER_FRESH;
    [self refreshData];
}

//******************************************************
// Method:     egoRefreshTableHeaderDidTriggerRefresh
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   向上拖拽刷新
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view
{
    //[self refreshData];
    //NSLog(@"get more data");
	//[self reloadTableViewDataSource];
	//[self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:4.0];
}

#pragma mark -
#pragma mark Content Filtering
//******************************************************
// Method:     filterContentForSearchText
// Access:     public 
// Returns:    void
// Parameter:  searchText scope
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{

}
#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods

//******************************************************
// Method:     searchDisplayController
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    NSArray* strscopeButtonTitles = [self.searchDisplayController.searchBar scopeButtonTitles];
    int index = [self.searchDisplayController.searchBar selectedScopeButtonIndex];
    
    NSString * strScope = @"";
    if (index < [strscopeButtonTitles count]) 
    {
        strScope =  [strscopeButtonTitles objectAtIndex:index];
    }
    
    [self filterContentForSearchText:searchString scope:strScope];
    return YES;
}

//******************************************************
// Method:     searchDisplayController
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    NSString* searchText = [self.searchDisplayController.searchBar text];
    NSArray* strscopeButtonTitles = [self.searchDisplayController.searchBar scopeButtonTitles];
    
    NSString * searchScope = @"";
    if (searchOption < [strscopeButtonTitles count]) 
    {
        searchScope =  [strscopeButtonTitles objectAtIndex:searchOption];
    }
    
    if ([searchScope compare:@"邮件标题"] == 0)
    {
        m_searchScopeType = @"01";
    }else
    {
        m_searchScopeType = @"02";
    }
    
    [self filterContentForSearchText:searchText scope:searchScope ];

    return YES;
}

//******************************************************
// Method:     searchDisplayControllerDidBeginSearch
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
	[self.searchDisplayController.searchResultsTableView setDelegate:self];
     m_searchScopeType = @"01";
}

//******************************************************
// Method:     searchDisplayControllerDidEndSearch
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    //Hide the search bar
	[self.tableView setContentOffset:CGPointMake(0, 44.f) animated:YES];
}

//******************************************************
// Method:     searchBarSearchButtonClicked
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
	m_searchText = [[NSString alloc] initWithFormat:@"%@",searchBar.text];
    [self doSearch];
}
//******************************************************
// Method:     searchBarTextDidBeginEditing
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    m_mailSearchBar.showsCancelButton = YES;
    for(id cc in [m_mailSearchBar subviews])
    {
        if([cc isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)cc;
            [btn setTitle:@"取消"  forState:UIControlStateNormal];
        }
    }
    if (self.m_mailSearchBar.showsCancelButton == NO)
    {
        self.m_mailSearchBar.showsCancelButton = YES;
    }
}

//******************************************************
// Method:     searchBarCancelButtonClicked
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    self.m_mailSearchBar.showsCancelButton = NO;
    [m_mailSearchBar resignFirstResponder];
    //[self refreshData];
}
#pragma mark - my function
//******************************************************
// Method:     doSearch
// Access:     public 
// Returns:    void
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (void)doSearch
{
    if(m_searchText.length<1)
        return;
    
    self.m_mailSearchBar.showsCancelButton=NO;
    
    m_bIsSearching = YES;
   // [responseParams removeAllObjects];
    
    [m_mailSearchBar resignFirstResponder];
    [self.searchDisplayController setActive:NO];
    [self.tableView setContentOffset:CGPointMake(0, 44.f) animated:YES];
    
    [self.m_table reloadData];
    
    [[[TaskManager Share] GetTaskByType:GetMailList Delegate:self] Excute];
}

//******************************************************
// Method:     tableView
// Access:     public 
// Returns:    editingStyleForRowAtIndexPath
// Parameter:  
// Note:	   
// Author      muzongcun  2012/03/12 create
//*******************************************************
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

@end
